# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#
#   DataZoomer Markup Language Translator
#   Pass a text string with DZML (a Wiki-like language) in it and this function
#   will transform it into HTML
#

from dzutil import link_to, url_for, tag_for, form_for, multipart_form_for
import os.path
from os.path import exists

def starts_with(text,key):
    return text[:len(key)] == key

def ends_with(text,key):
    bare_key = key.strip()
    return text[-len(bare_key):] == bare_key.strip()

def format_field(label,content):
    tpl = \
"""
<div class="field">
  <div class="field_label">%(label)s</div>
  <div class="field_edit">
    %(content)s
  </div>
</div>"""
    return tpl % (dict(label=label,content=content))

def name_for(text):
    return text.replace('*','').replace(' ','_').strip().upper()

def field_for(text):
    q = text.split(' ',1)
    if len(q)>1:
        p = q[1].strip().split('|',1)
        if len(p)>1:
            name = p[0].strip()
            label = p[1].strip()
        else:
            name = name_for(p[0])
            label = p[0].strip()
        return (name,label,[],{})
    return ''


def dzmlt(text,form=0,site=None,folder='.',static=0,callback=None,view=None,values={},**keywords):

    def process_markup(text,begin,end,callback):
        def inline_markup(text,begin,end):
            s1 = text.split(begin,1)
            if len(s1)>1:
                s2 = s1[1].split(end,1)
                if len(s2)>1:
                    return (s1[0],s2[0],s2[1])
            return None
        parts = inline_markup(text,begin,end)
        while parts:
            text = callback(parts[0],parts[1],parts[2])
            parts = inline_markup(text,begin,end)
        return text

    def set_bold(start,middle,end):
        return '%s <B>%s</B>%s' % (start,middle,end)

    def set_italics(start,middle,end):
        return '%s <I>%s</I>%s' % (start,middle,end)

    def set_red(start,middle,end):
        return '%s<FONT COLOR=red>%s</FONT>%s' % (start,middle,end)

    def set_link(start,middle,end):
        p = middle.split('|',1)

        if len(p)>1:
            label = content = p[1]
        else:
            if '.' in p[0]:
                label = p[0].split('.',1)[1]
            else:
                label = p[0]

        page_name  = name_for(p[0]).lower()

        if starts_with(page_name,'http:') or starts_with(page_name,'https:'):
            return '%s<a href="%s">%s</a>%s' % (start,page_name,label,end)

        elif static:
            filename = os.path.join(folder,page_name+'.txt')
            if exists(filename):
                return '%s<a href="%s.htm">%s</a>%s' % (start,page_name,label,end)
            else:
                return '%s%s%s' % (start,label,end)

        elif callback:
            t = callback('a',page_name,label) or ''
            return start + t + end

        else:

#            filename = os.path.join(folder,page_name+'.txt')
            from zoom import zoomer

            if '.' in page_name:
                (con,action) = page_name.split('.',1)
            else:
                (con,action) = (zoomer.app.controller_name,page_name)

            if con:
                filename = os.path.join(folder,'%s_%s.txt' % (con,action))
            else:
                filename = os.path.join(folder,'%s.txt' % action)

            if exists(filename):
                link_label = label
            else:
                link_label = label + '<FONT COLOR=red>?</FONT>'

#            print 'page=',page_name, 'con=',con, 'action=',action, 'filename=',filename, 'label=',link_label

            if con:
                link = link_to(link_label,con=con,action=action)
            else:
                link = link_to(link_label,action=action)

            return '%s%s%s' % (start,link,end)

    def split_action_parts(line):
        (action_name,label,_,_) = field_for('. '+line)
        return (action_name.lower(),label)

    def render_button(before,content,after):
        p = content.split('|',1)
        button_name = name_for(p[0]).lower() + '_button'
        if len(p)>1:
            label = p[1]
        else:
            label = p[0]
        return ''.join([before,tag_for('input',Class='std_button',Type='submit',name=button_name,value=label),after])

    def render_no_yes_field(line):
        p = line[3:].split('|',1)
        field_name = name_for(p[0])
        if len(p)>1:
            label = p[1].strip()
        else:
            label = p[0].strip()
        default_value = None

        current_value = view and view.get(field_name,None)
        if current_value == None:
            value = default_value
        else:
            value = current_value

        if value=='1':
            yes_checked = 'CHECKED'
            no_checked = ''
        elif value=='0':
            no_checked = 'CHECKED'
            yes_checked = ''
        else:
            yes_checked = no_checked = ''

        tpl = format_field(label,\
"""
    <input class=radio type=radio name="%(field_name)s" value="0" %(no_checked)s />No&nbsp;
    <input class=radio type=radio name="%(field_name)s" value="1" %(yes_checked)s />Yes
""")
        d = dict(label=label,field_name=field_name,yes_checked=yes_checked,no_checked=no_checked)
        return tpl % (d)

    def render_yes_no_field(line):
        p = line[3:].split('|',1)
        field_name = name_for(p[0])
        if len(p)>1:
            label = p[1].strip()
        else:
            label = p[0].strip()
        default_value = None
        current_value = view and view.get(field_name,None)
        if current_value == None:
            value = default_value
        else:
            value = current_value

        if value=='1':
            yes_checked = 'CHECKED'
            no_checked = ''
        elif value=='0':
            no_checked = 'CHECKED'
            yes_checked = ''
        else:
            yes_checked = no_checked = ''

        tpl = format_field(label,\
"""
    <input class=radio type=radio name="%(field_name)s" value="1" %(yes_checked)s />Yes&nbsp;
    <input class=radio type=radio name="%(field_name)s" value="0" %(no_checked)s />No
""")
        d = dict(label=label,field_name=field_name,yes_checked=yes_checked,no_checked=no_checked)
        return tpl % (d)

    def render_hidden_field(line):
        p = line[3:].split('|',1)
        field_name = name_for(p[0])
        if form:
            tpl = '<input type=hidden name="%(name)s" value="<#%(value)s>">'
        else:
            tpl = ''
        return tpl % (dict(name=field_name,value=field_name))

    def render_password_field(line):
        p = line[3:].split('|',1)
        field_name = name_for(p[0])
        if len(p)>1:
            label = p[1].strip()
        else:
            label = p[0].strip()

        if form:
            tpl = format_field(label,'<input class=password type=password size=20 maxlength=40 name="%(name)s" value="<#%(value)s>">')

        return tpl % (dict(label=label,name=field_name,value=field_name))

    def render_checkbox(line):
        p = line[3:].split('|',1)
        field_name = name_for(p[0])
        if len(p)>1:
            label = p[1].strip()
        else:
            label = p[0].strip()

        checked = view and view.get(field_name,None) == '1' and 'CHECKED' or ''

        if form:
            tpl = format_field(label,'<input class=checkbox type=checkbox name="%(name)s" %(checked)s value="1">')
        else:
            tpl = format_field(label,'<input class=checkbox type=checkbox name="%(name)s" %(checked)s value="1">')

        return tpl % (dict(label=label,name=field_name,checked=checked))

    def render_text_field(line):
        size = prefix_size(line)
        p = line[size+1:].split('|',1)
        if len(p)>1:
            field_name = p[0]
            label = p[1].strip()
        else:
            field_name = name_for(p[0])
            label = p[0].strip()

        view_value = view and view.get(field_name)
        if view_value != None:
            value = view_value
        else:
            value = '<#%s>' % field_name

        if form:
            if size==1:
                tpl = format_field(label,'<input class=text type=text size=20 maxlength=40 name="%(name)s" value="%(value)s">')

            elif size==2:
                tpl = format_field(label,'<input class=text type=text size=40 maxlength=80 name="%(name)s" value="%(value)s">')

            elif size==3:
                tpl = format_field(label,'<textarea name="%(name)s">%(value)s</textarea>')

            else:
                tpl = format_field(label,'<textarea class=large name="%(name)s">%(value)s</textarea>')

        else:
            tpl = format_field(label,'<strong>%(value)s</strong>')

        return tpl % (dict(label=label,name=field_name,value=value))

    def set_paragraph(tag):
        text = tag.groups()[0]
        print ord(text)
        if len(text)>1:
            return '<BR />'*len(text)
        else:
            return ' '

    def set_paragraphs(text):
        n = len(text)
        result = []
        p1 = text.find('\n\n')
        p2 = 0
        while p1 > -1:
            result.append(text[p2:p1])
            p2 = p1
            while p2<n and text[p2]=='\n': p2 += 1
            if p2-p1 > 1:
                result.append('<BR />' * (p2-p1))
            else:
                result.append('\n')
            p1 = text.find('\n\n',p2)
        result.append(text[p2:])
        t = ''.join(result)
        return t

    def prefix_size(start):
        s = 1
        while s<len(start) and start[0]==start[s]:
            s += 1
        return s

    def process_line_start(line,starter,tag):
        ps = prefix_size(line)
        if starter == line[0]:
            return '<H%d>%s</H%d>'%(ps,line[ps:-1],ps)
        return line

    in_table = in_list = in_radio = in_pulldown = in_radio = 0
    
    pulldown_label   = ''
    pulldown_name    = ''
    pulldown_options = []

    result = []

    for line in text.splitlines(1):
        if starts_with(line,'.'):
            if len(line)>len('.action ') and starts_with(line[1:],'action ') and callback:
                action,label = split_action_parts(line.split('.action ',1)[1])
                callback('action',action,label)

    for line in text.splitlines(1):

        if starts_with(line,'.'):
            continue

        # inline images
        for word in text.split():
            if word[-4:].lower() in ['.jpg','.png','.gif']:
                if starts_with(word,'http:') or starts_with(word,'https:'):
                    line = line.replace(word,'<img src="%s">' % word)
                else:
                    line = line.replace(word,'<img src="content/images/%s">' % word)

        # End of composite element
        if starts_with(line,'*'):
            level = prefix_size(line)
        else:
            level = 0
        while in_list > level:
            in_list -= 1
            result.append('%s</UL>\n' % (in_list*' '))

        # Pulldowns
        if starts_with(line,'?o'):# and not starts_with(line,'----'):
            if not in_pulldown:
                in_pulldown = 1
                (pulldown_name,pulldown_label,o,k) = field_for(line)
                pulldown_options = []
                continue
            else:
                t = line[2:].split('|',1)
                name  = t[0].strip()
                label = (len(t)>1 and t[1] or t[0]).strip()
                value = view and view.get(pulldown_name,'')
                if value==name:
                    pulldown_options.append('<OPTION SELECTED VALUE="%(value)s">%(label)s</OPTION>'%dict(value=name,label=label))
                else:
                    pulldown_options.append('<OPTION VALUE="%(value)s">%(label)s</OPTION>'%dict(value=name,label=label))
                continue
        else:
            if in_pulldown:
                in_pulldown = 0
                result.append(format_field(pulldown_label,tag_for('select',''.join(pulldown_options),Class='select',name=pulldown_name)))
                continue

        # indent (for buttons, okay, it's a kludge)
        if starts_with(line,'>>'):
            line = format_field('&nbsp;',line[2:])

        # markup that happens within a line
        line = process_markup(line," **","**",set_bold)
        line = process_markup(line," //","//",set_italics)
        line = process_markup(line,"%red%","%red%",set_red)
        line = process_markup(line,'[[',']]',set_link)
        line = process_markup(line,'[-','-]',render_button)
        
        # markup that happens at the start of a line
        if starts_with(line,'!'):
            result.append( process_line_start(line,'!','H') )

        elif starts_with(line,'----'):
            result.append('<HR />' + line[4:])

        elif starts_with(line,'?n'):
            result.append(render_no_yes_field(line))
            result.append('\n')

        elif starts_with(line,'?y'):
            result.append(render_yes_no_field(line))
            result.append('\n')

        elif starts_with(line,'?p'):
            result.append(render_password_field(line))
            result.append('\n')

        elif starts_with(line,'?x'):
            result.append(render_checkbox(line))
            result.append('\n')

        elif starts_with(line,'?'):
            result.append(render_text_field(line))
            result.append('\n')

        elif starts_with(line,'[-'):
            result.append(render_hidden_field(line))
            result.append('\n')

        elif starts_with(line,'*'):
            level = prefix_size(line)
            while in_list < level:
                indent = in_list*' '
                in_list += 1
                result.append('%s<UL class="level%s">\n' % (indent,in_list))
            result.append('%s<LI class="level%s">%s</LI>\n' % (level*' ',level,line[level:].replace('\n','')))

        elif ends_with(line,'>'):
            result.append(line.replace('\n','\n'))

        else:
            result.append(line.replace('\n','<BR />\n'))

    # End of composite element
    while in_list:
        in_list -= 1
        result.append('%s</UL>\n' % (in_list*' '))

    return result


def dzml(text,site=None,form=0,folder='.',static=0,**keywords):

    def form_is_required(text):
        for line in text.splitlines(1):
            if starts_with(line,'.form'):
                return 1

    form_required = 1

    result = []

    text = text.replace('\r\n','\n')

    p1 = text.find('{{{')
    p2 = 0
    while p1>-1:
        text_to_process = text[p2:p1]
        result += dzmlt(text_to_process,form,folder=folder,**keywords)

        p2 = text.find('}}}',p1+3)
        if p2>0:
            result.append(text[p1+3:p2])
            p2 = p2 + 3
            if text[p2]=='\n':
                p2 += 1
            p1 = text.find('{{{',p2+3)
        else:
            p1 = -1
            p2 = len(text)

    text_to_process = text[p2:]
    result += dzmlt(text_to_process,form,folder=folder,**keywords)
    
    params = {}
    if 'action' in keywords:
        params.setdefault('action',keywords.get('action'))
    if 'con' in keywords:
        params.setdefault('con',keywords.get('con'))
    elif 'form_name' in keywords:
        params.setdefault('con',keywords.get('form_name'))

    if form==1 and form_required:
        content = form_for(''.join(result),**params)
    elif form==2 and form_required:
        content = multipart_form_for(''.join(result),**params)
    else:
        content = ''.join(result)

    return content


#    return ''.join(result)).replace('\n','<BR />\n')
#    return set_paragraphs(''.join(result))
#    return ''.join(result)

#         t = ''
#         if not beginswith(text,'*') and inlist:
#             while inlist:
#                 t += '</ul>'
#                 inlist -= 1
#
#         if not beginswith(text,'||') and intable:
#             t += '</table>'
#             intable = 0
#
#         for word in text.split():
#             if word[-4:].lower() in ['.jpg','.png','.gif']:
#                 text = text.replace(word,'<img src=%s>' % word)
#
#         text = process_markup(text,"[[","]]",set_link)
#
#         elif beginswith(text,'*'):
#
#             level = 1
#             if beginswith(text,'***'):
#                 level = 3
#             elif beginswith(text,'**'):
#                 level = 2
#
#             while inlist < level:
#                 inlist += 1
#                 t += '<ul class="level%s">' % level
#
#             while inlist > level:
#                 inlist -= 1
#                 t += '</ul>'
#
#             if beginswith(text,'***'):
#                 t += '<li class="level3">'+text[3:]+'</li>'
#             elif beginswith(text,'**'):
#                 t += '<li class="level2">'+text[2:]+'</li>'
#             elif beginswith(text,'*'):
#                 t += '<li class="level1"">'+text[1:]+'</li>'
#
#         elif beginswith(text,'||'):
#             if not intable:
#                 t += '<table %s class=usertable width="100%%" border=1>' % text[2:]
#                 intable = 1
#             else:
#                 if text.strip()[-2:] == '||':
#                     core = text.strip()[2:-2]
#                 else:
#                     core = text[2:]
#                 q = qml()
#                 core = qml().qmltext(core)
#                 t += '<tr><td class=usertable>'+core.replace('||','</td><td class=usertable>')+'</td></tr>'
#
#         else:
#             t += text # + '<br>'
#
#         return t
#    return text

if __name__ == '__main__':
    import unittest

    class DZMLTest(unittest.TestCase):

        def t(self,src,tar):
#            for x,y in zip(dzml(src).splitlines(),tar.splitlines()):
#                self.assertEqual(x,y)
            context = {'SID':'mysid','app':'dzml'}
            self.assertEqual(dzml(src,context,view={}),tar)

        def t2(self,src,tar):
            values = {}
            t1 = dzml(src,view=values).splitlines()
            t2 = tar.splitlines()
            for i in range(len(t1)):
                self.assertEqual('%s: %s' % (i,t1[i]),'%s: %s' % (i,t2[i]))

        def test_text(self):
            self.t('test123','test123')

        def test_paragraph(self):
            self.t('test123\nline2','test123<BR />\nline2')
            self.t('test123\n\nline3','test123<BR />\n<BR />\nline3')
            self.t('test123\n\nline5','test123<BR />\n<BR />\nline5')
            self.t('test123\n\nline6\n','test123<BR />\n<BR />\nline6<BR />\n')
            self.t('test123\n\nline7\n\n','test123<BR />\n<BR />\nline7<BR />\n<BR />\n')
            self.t('test123\n\n\nline8\n','test123<BR />\n<BR />\n<BR />\nline8<BR />\n')
            self.t('test123\n\n\nline9\n\n','test123<BR />\n<BR />\n<BR />\nline9<BR />\n<BR />\n')
            self.t('test123\n\n\nline10\n\npart2','test123<BR />\n<BR />\n<BR />\nline10<BR />\n<BR />\npart2')
            self.t('test123\n\n\nline11\n\npart2\n','test123<BR />\n<BR />\n<BR />\nline11<BR />\n<BR />\npart2<BR />\n')
            self.t('test123\n\n\nline12\n\npart2\n\n\n','test123<BR />\n<BR />\n<BR />\nline12<BR />\n<BR />\npart2<BR />\n<BR />\n<BR />\n')

        def test_bold(self):
            self.t('tes **t1** 23','tes <B>t1</B> 23')

        def test_italics(self):
            self.t('tes //t1// 23','tes <I>t1</I> 23')
            self.t('test http://what.com //is it//','test http://what.com <I>is it</I>')

        def test_html(self):
            self.t('!This\n {{{**not bold**}}} **is bold** {{{**not again**}}} **is**','<H1>This\n</H1> **not bold** <B>is bold</B> **not again** <B>is</B>')
            self.t('!This is a test\n{{{* And this is\n* not bulleted}}}\n*this is','<H1>This is a test\n</H1>* And this is\n* not bulleted<BR />\n<UL class="level1">\n <LI class="level1">this is</LI>\n</UL>\n')
            self.t(' {{{This is \nleft exactly\n!Like it is\n*now}}}',
                ' This is \nleft exactly\n!Like it is\n*now',)
            self.t('{{{This is \nleft exactly\n!Like it is\n*now}}}',
                'This is \nleft exactly\n!Like it is\n*now',)

        def test_not_italics(self):
            self.t('tes //t1 23','tes //t1 23')
            self.t('test http://what.com is it','test http://what.com is it')
            self.t('test http://what.com is it or http://what.com','test http://what.com is it or http://what.com')

        def test_red(self):
            self.t('tes %red%t1%red% 23','tes <FONT COLOR=red>t1</FONT> 23')

        def test_image(self):
            self.t('image.jpg','<img src="content/images/image.jpg">')
            self.t('this is an image.jpg and so is this.gif image',\
                'this is an <img src="content/images/image.jpg"> and so is <img src="content/images/this.gif"> image')
            self.t('image.jpg and so\nis this.jpg image',\
                '<img src="content/images/image.jpg"> and so<BR />\nis <img src="content/images/this.jpg"> image')
            self.t('image.jpg\nthis.jpg',\
                '<img src="content/images/image.jpg"><BR />\n<img src="content/images/this.jpg">')
            self.t('this is an image.jpg and so\nis this.jpg image',\
                'this is an <img src="content/images/image.jpg"> and so<BR />\nis <img src="content/images/this.jpg"> image')

        def test_header(self):
            self.t('!Header1','<H1>Header1</H1>')
            self.t('!!Header2','<H2>Header2</H2>')
            self.t('!!!Header3','<H3>Header3</H3>')
            self.t('!!!!Header4','<H4>Header4</H4>')
            self.t('!!!!Header4\n!!Header2','<H4>Header4\n</H4><H2>Header2</H2>')
            self.t('test\n\n!Header5','test<BR />\n<BR />\n<H1>Header5</H1>')

        def test_button(self):
            self.assertEqual(dzml('[-Save-]'),'<INPUT TYPE="submit" CLASS="std_button" VALUE="Save" NAME="save_button">')

        def test_field(self):
            def span(cls,txt):
                return '<span class="%s">%s</span>' % (cls,txt)
            def div(cls,txt):
                return '\n<div class="%s">%s</div>' % (cls,txt)
            self.assertEqual(dzml('? Test Field',TEST_FIELD='one'),div('field',div('field_label','Test Field')+div('field_value','one')))
            #'\n<span class="field">\n <span class="field_label">Test Field</n><input TYPE="text" name="test_field" value="one">')
#            self.assert_('<input TYPE="text" name="test_field" value="one">' in dzml('? Test Field',test_field='one'))

        def test_pulldown(self):
            self.t2(\
"""?o City
?o Rome
?o Paris
?o Barcelona

""",format_field('City','<SELECT NAME="CITY"><OPTION VALUE="Rome">Rome</OPTION><OPTION VALUE="Paris">Paris</OPTION><OPTION VALUE="Barcelona">Barcelona</OPTION></SELECT>'))


        def test_line(self):
            self.t('----','<HR />')
            self.t('\n----\n','<BR />\n<HR />\n')
            self.t('test\n----\ntest','test<BR />\n<HR />\ntest')
            self.t('!Header\n----\ntest','<H1>Header\n</H1><HR />\ntest')

        def test_bullet(self):
            self.t2('*Bullet1','<UL class="level1">\n <LI class="level1">Bullet1</LI>\n</UL>')

        def test_bullet2(self):
            self.t2('*Bullet1\n*Bullet2','<UL class="level1">\n <LI class="level1">Bullet1</LI>\n <LI class="level1">Bullet2</LI>\n</UL>')

        def test_bullet3(self):
            self.t2('*Bullet1\n**Bullet2','<UL class="level1">\n <LI class="level1">Bullet1</LI>\n <UL class="level2">\n  <LI class="level2">Bullet2</LI>\n </UL>\n</UL>\n')

        def test_bullet4(self):
            self.t2(\
"""Test
* Bullet1
** Bullet2
*** Bullet3
*** Bullet3b
**Bullet2b
*Bullet1b
****Bullet4
*Bullet1
""",
"""Test<BR />
<UL class="level1">
 <LI class="level1"> Bullet1</LI>
 <UL class="level2">
  <LI class="level2"> Bullet2</LI>
  <UL class="level3">
   <LI class="level3"> Bullet3</LI>
   <LI class="level3"> Bullet3b</LI>
  </UL>
  <LI class="level2">Bullet2b</LI>
 </UL>
 <LI class="level1">Bullet1b</LI>
 <UL class="level2">
  <UL class="level3">
   <UL class="level4">
    <LI class="level4">Bullet4</LI>
   </UL>
  </UL>
 </UL>
 <LI class="level1">Bullet1</LI>
</UL>""")

        def stest_links1(self):
            self.t('[[Test]]','<a href="?sid=<#SID>&app=<#APP>&action=test">Test</a>')

        def stest_links2(self):
            self.t('[[test_site|Test]]','<a href="?sid=<#SID>&app=<#APP>&action=test_site">Test</a>')

        def stest_links3(self):
            self.t('[[Test Site|Tester]]','<a href="?sid=<#SID>&app=<#APP>&action=test_site">Tester</a>')

        def stest_links4(self):
            l1 = '[[salutation|Salutation (Mr.,Mrs.)]]'
            l2 = '<a href="/index?sid=<#SID>&app=<#APP>&action=salutation">Salutation (Mr.,Mrs.)</a>'
            try:
                self.assertEqual(dzml(l1),l2)
            except:
                print dzml(l1,sid='nosid')
                print l2

        def test_field_for(self):
            self.assertEqual(field_for('test'),'')
            self.assertEqual(field_for('.test'),'')
            self.assertEqual(field_for('test one'),('ONE','one',[],{}))
            self.assertEqual(field_for('.test one'),('ONE','one',[],{}))
            self.assertEqual(field_for('.test one|name'),('one','name',[],{}))
            self.assertEqual(field_for('.test one two|name'),('one two','name',[],{}))
            self.assertEqual(field_for('.test one two|name three'),('one two','name three',[],{}))

#            self.t('[[salutation|Salutation (Mr.,Mrs.)]]','<a href="?sid=<#SID>&app=<#APP>&action=salutation">Salutation (Mr.,Mrs.)</a>')




#             t1 = dzml(x[0]).splitlines()
#             t2 = x[1].splitlines()
#             for i in range(len(t1)):
#                 self.assertEqual('%s: %s' % (i,t1[i]),'%s: %s' % (i,t2[i]))

#         def test_text(self):
#             p = dzml()
#             self.assertEqual(p.qmltext("this is a test"),"this is a test")
#
#         def test_convert(self):
#             p = dzml()
#             self.assertEqual(p.convert("this is\n\na '''test'''."),"this is<br>a <b>test</b>.")
#
#         def test_link(self):
#             p = dzml()
#             self.assertEqual(p.qmltext('this link [[Do This]] is a test'),'this link <a href="?sid=<#SID>&app=<#APP>&action=do_this">Do This</a> is a test')
#
#         def test_paragraph(self):
#             p = dzml()
#             self.assertEqual(p.qml('This is para1\n\nthis is para2')['PANEL1'],'This is para1<br>this is para2')
#             self.assertEqual(p.qml('This is para1\n\n\nthis is para2')['PANEL1'],'This is para1<br><br>this is para2')
# #            self.assertEqual(p.qml('This is para1\n\n\n\nthis is para2')['PANEL1'],'This is para1<br><br><br>this is para2')
#
#         def test_panels(self):
#             p = dzml()
#             self.assertEqual(p.qml("text1\n====\ntext2"),{'PANEL1':'text1','PANEL2':'text2'})
#
#         def test_bullets(self):
#             p = dzml()
#             self.assertEqual(p.qml("Test\n* One\n*Two\n* Three\n"),{'PANEL1':'Test<ul><li> One</li><li>Two</li><li> Three</li></ul>'})
#             self.assertEqual(p.qmltext("Test\n* One\n*Two\n* Three\n"),'Test<ul><li> One</li><li>Two</li><li> Three</li></ul>')

    unittest.main()


